#include "loginform.h"
#include "ui_loginform.h"

#include "flowcontroler.h"
#include "registeruserdialog.h"

#include <QMessageBox>

LoginForm::LoginForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginForm)
{
    ui->setupUi(this);

    XInterface *interface = FlowControler::Interface;
    connect(interface, &XInterface::loginFinished, this, [=](int ret)
    {
        if(ret == NoError)
        {
            emit loginSuccess(interface->getCurrentUser()->name, 0);
            accept();
        }
        else
        {
            QMessageBox::warning(this, "警告", GetErrorMessage(ret));
        }
    });
}

LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::on_btnLogin_clicked()
{
    Core::User user;
    user.name = ui->editUser->text();
    user.password = ui->editPswd->text();
    FlowControler::Interface->login(user);

}

void LoginForm::on_btnRegister_clicked()
{
    RegisterUserDialog dialog;
    dialog.exec();
}
